﻿/*
作者：江怀玉
组织：iSun Studio
功能说明：定义通用的列表类
日期：2012-08-13
修改：江怀玉
      2013-01-02
      增加行选择，数据刷新功能
*/
//定义一个iSun.common.list类
var _xhr;
var _SelectItems;
require([
				"dojox/grid/DataGrid",
				"dojo/store/Memory",
				"dojo/data/ObjectStore",
				"dojo/_base/xhr",
                "dojox/grid/cells", "dojo/_base/array", "dojo/_base/lang", "dojox/grid/_CheckBoxSelector", "dojox/grid/_RadioSelector",
				"dojo/domReady!"
			], function (DataGrid, Memory, ObjectStore, xhr, cells, baseArray, lang) {
			    _xhr = xhr;
			    _xhr.get({
			        url: _dataJSON,
			        handleAs: "json"
			    }).then(function (data) {
			        store = new Memory({ data: data.items });
			        dataStore = new ObjectStore({ objectStore: store });

			        grid = new DataGrid({
			            store: dataStore,
			            query: { id: "*" },
			            structure: [
			            // First, our view using the _CheckBoxSelector custom type
					        {
					        type: "dojox.grid._CheckBoxSelector"
					    },
                            [_cells]]
			        }, "grid");

			        //bind event
			        function gridSelection(node) {
			            var items = this.selection.getSelected();
			            _SelectItems = items;
			            var tmp = baseArray.map(items, function (item) {
			                //debugger
			                //alert(eval("item."+const_entityID));
			                //alert(item.RoleName);
			                return item;
			            }, this);
			            //var msg = "You have selected row" + ((tmp.length > 1) ? "s " : " ");
			            //node.innerHTML = msg + tmp.join(", ");
			        }
			        grid.on("SelectionChanged",
					lang.hitch(grid, gridSelection), true);



			        //刷新数据
			        function ReoladData(data) {
			            // You can refresh a grid by updating the json store behind it
			            store = new Memory({ data: data.items });
			            dataStore = new ObjectStore({ objectStore: store });
			            dataStore.fetch({
			                query: { id: "*" },
			                onComplete: function (items, result) {
			                    // And then calling sort on the grid
			                    grid.sort();

			                }
			            });
			        }

			        //function   
			        // since we created this grid programmatically, call startup to render it
			        grid.startup();
			    });
			});



			var refresh = function () {
			    newStore = new dojo.data.ItemFileReadStore(store);
			    grid = dijit.byId("grid");
			    grid.setStore(newStore);
			    grid.sort();

			    //    grid.store.close();
			    //    grid.store = new dojo.data.ItemFileReadStore({ url: "whatever." + variable + ".json", clearOnClose: true });
			    //    grid.sort();
			}

			var getSelectItems = function (_grid) {
			    //debugger;
			    var items = _grid.selection.getSelected();
			    _SelectItems = items;
			    if (items.length) {
			        // Iterate through the list of selected items.
			        // The current item is available in the variable
			        // "selectedItem" within the following function:
			        dojo.forEach(items, function (selectedItem) {
			            if (selectedItem !== null) {
			                // Iterate through the list of attributes of each item.
			                // The current attribute is available in the variable
			                // "attribute" within the following function:
			                dojo.forEach(grid2.store.getAttributes(selectedItem), function (attribute) {
			                    // Get the value of the current attribute:
			                    var value = grid2.store.getValues(selectedItem, attribute);
			                    // Now, you can do something with this attribute/value pair.
			                    // Our short example shows the attribute together
			                    // with the value in an alert box, but we are sure, that
			                    // you'll find a more ambitious usage in your own code:
			                    alert('attribute: ' + attribute + ', value: ' + value);
			                }); // end forEach
			            } // end if
			        }); // end forEach
			    } // end if

			    return items;

			}
//参考
//http: //dojotoolkit.org/documentation/tutorials/1.8/working_grid/
//http://dojotoolkit.org/documentation/tutorials/1.8/store_driven_grid/
//工具栏

//iSun.toolbar = {
//} 


//require([
//				"dojo/store/JsonRest",
//				"dojo/store/Memory",
//				"dojo/store/Cache",
//				"dojox/grid/DataGrid",
//				"dojo/data/ObjectStore",
//				"dojo/query",
//				"dojo/domReady!"
//			], function (JsonRest, Memory, Cache, DataGrid, ObjectStore, query) {
//			    var myStore, dataStore, grid;
//			    myStore = Cache(JsonRest({ target: "MyData/" }), Memory());
//			    grid = new DataGrid({
//			        store: dataStore = new ObjectStore({ objectStore: myStore }),
//			        structure: [
//						{ name: "State Name", field: "name", width: "200px" },
//						{ name: "Abbreviation", field: "abbreviation", width: "200px", editable: true }
//					]
//			    }, "target-node-id"); // make sure you have a target HTML element with this id
//			    grid.startup();
//			    query("#save").onclick(function () {
//			        dataStore.save();
//			    });
//			});